﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;
using NHibernate;
using TaxiCaosSOAPService.Dominio;

namespace TaxiCaosSOAPService.Persistencia
{
    public class PagoDAO : BaseDAO<Pago, int>
    {

        public string ObtenerImporte(string nroReserva)
        {
            try
            {
                using (ISession sesion = NHibernateHelper.ObtenerSesion())
                {
                    string sentenciaSQL = "select MontoNeto from Factura where IdReserva = :IdReserva";
                    IQuery query = sesion.CreateQuery(sentenciaSQL);
                    query.SetParameter("IdReserva", int.Parse(nroReserva));
                    IList<double> monto = query.List<double>();
                    return monto[0].ToString("0.00", CultureInfo.InvariantCulture);
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }



        public int RegistrarPago(int idPago, string banco, string ctabancaria, string moneda, string monto)
        {
            try
            {
                using (ISession sesion = NHibernateHelper.ObtenerSesion())
                {
                    string sentenciaSQL = "insert into Pago values (:IdPago , :Banco, :CtaBancaria, :Moneda, :Monto)";

                    IQuery query = sesion.CreateSQLQuery(sentenciaSQL);
                    query.SetParameter("IdPago", idPago);
                    query.SetParameter("Banco", banco);
                    query.SetParameter("CtaBancaria", ctabancaria);
                    query.SetParameter("Moneda", moneda);
                    query.SetParameter("Monto", monto);
                    query.ExecuteUpdate();
                    return idPago;
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }


        public bool ActualizarFacturaConPago(string nroReserva, int idPago)
        {
            try
            {
                using (ISession sesion = NHibernateHelper.ObtenerSesion())
                {
                    string sentenciaSQL = "update Factura set IdPago = :idPago where idReserva = :nroReserva";
                    IQuery query = sesion.CreateQuery(sentenciaSQL);
                    query.SetParameter("nroReserva", int.Parse(nroReserva));
                    query.SetParameter("idPago", idPago);
                    query.ExecuteUpdate();
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }



        public int ObtenerNuevoIDPago()
        {
            try
            {
                using (ISession sesion = NHibernateHelper.ObtenerSesion())
                {
                    string sentenciaSQL = "select isNull(max(IdPago),0) IdPago from Pago";
                    IQuery query = sesion.CreateSQLQuery(sentenciaSQL);
                    IList<int> maxID = query.List<int>();
                    return maxID[0] + 1;
                }
            }
            catch (Exception ex)
            {

                return 0;
            }

        }
    }
}